package 作业;


import java.io.*;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

//存储数据，使用反序列存储
public class userinformation {
    public data datas;
    public Set<String> selfticket;
    public String name;
    public String password;
    public String path="info.txt";
    private File f;
    public userinformation() throws IOException, ClassNotFoundException {//唯一的构造方法
        f=new File(path);
        if(!f.exists()||f.length()<=4){
            f.createNewFile();
            datas=new data();
        }
        else{
            ObjectInputStream temp=new ObjectInputStream(new FileInputStream(f));
            datas=(data)temp.readObject();
            temp.close();
        }
    }
    public boolean check(String name,String password) throws SQLException {
        String patten=Getdb.op.getuser(name);
        if(patten.length()==0)
            return false;
        else{
            if (password.equals(patten))
                return true;
        }
        return false;
//        if(datas.up.containsKey(name)) {
//            if(datas.up.get(name).equals(password)) {
//                this.name=name;
//                this.password=password;
//                this.selfticket=datas.ut.get(name);
//                return true;
//            }
//        }
//        return false;
    }

    public boolean newuser(String name,String password) throws IOException, SQLException {
        //data是原来中已存在得数据

        String ans=Getdb.op.getuser(name);
        if(ans.length()==0){
            Getdb.op.AddUser(name,password);
            return true;
        }
        else
            return false;
    }

    public String gettype(String name,String pass) throws SQLException {
        return Getdb.op.getuser(name,pass);
    }
    public void edpassword(String pass) throws IOException {
        this.datas.up.put(this.name, pass);
        ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(f));
        o.writeObject(datas);
        o.flush();
        o.close();
    }

    public String[] geth() {//获取历史记录
        String top="发车时间                   起点站              终点站          价格         购买时间";
        String [] ans=new String[selfticket.size()+1];
        ans[0]=top;
        String [] temp=selfticket.toArray(new String[selfticket.size()]);
        for(int i=0;i<selfticket.size();i++) {
            ans[i+1]=temp[i];
        }
        return ans;
    }

    public boolean del(String t) throws IOException {//删除操作用
        boolean flag;
        flag=selfticket.remove(t);
        if(flag) {
            this.datas.ut.put(name,selfticket);
            ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(f));
            o.writeObject(datas);
            o.flush();
            o.close();
        }
        return flag;
    }

    public boolean add(String t) throws IOException {//添加作用
        DateFormat df=new SimpleDateFormat("YYYY年MM月dd日HH时mm分");
        t=t+df.format(new Date(System.currentTimeMillis()));
        selfticket.add(t);
        datas.addticket(this.name,t);
        ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(f));
        o.writeObject(datas);
        o.flush();
        o.close();
        return true;
    }
}
class data implements Serializable {//存储数据类
    static final long serialVersionUID =1L;
    public Map<String,String> up;//用户名和对应密码
    public Map<String, Set<String>> ut;//用户对应车票记录
    public data(){//如果没有序列就执行该方法
        up=new HashMap<>();
        ut=new HashMap<>();
    }
    public void addticket(String name,String ticket){
        Set<String> temp=ut.get(name);
        temp.add(ticket);
        ut.put(name,temp);
    }
}
